package org.itheima.controller;

import org.apache.ibatis.annotations.Delete;
import org.itheima.pojo.Result;
import org.itheima.service.ShouCangService;
import org.itheima.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shouCang")
public class ShouCangController {

    @Autowired
    private ShouCangService shouCangService;

    @PostMapping("/addShouCang")
    public void addShouCang(@RequestBody Map<String, String> request) {
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        String name = request.get("name");
        String price = request.get("price");
        String image = request.get("image");
        int tableId = Integer.parseInt(request.get("tableId"));
        shouCangService.addShouCang(name,username,price,image,tableId);
    }

    @GetMapping("/shouCangFindById")
    public List<Map<String, Object>> shouCangFindById(){
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        List<Map<String, Object>> response = shouCangService.shouCangFindById(username);
        return response;
    }

    @DeleteMapping("/deleteShouCang")
    public void deleteShouCang( @RequestParam String name) {
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        shouCangService.deleteShouCang(name,username);
    }
}
